<?php
	class _init extends CI_Controller
	{
		protected function set_header_main($membercp=true,$logo=true,$footer=true){			
			if ($membercp){
				// facebook
				$this->load->library('facebook_api');			
				$data_menu_top['app_id'] = $this->facebook_api->facebook_app_id;
				$data_menu_top['fanpage_id'] = $this->facebook_api->facebook_fanpage_id;
				$data_menu_top['logged_in'] = false;
	
				$logged_in = $this->session->userdata('IS_LOGGED_IN');			
				if (isset($logged_in) && $logged_in){
					
					$fb_user_id = $this->session->userdata('FB_USER_ID');
					if (empty($fb_user_id))
						$data_menu_top['user']['avatar'] = $this->facebook_api->get_user_avatar();
					else
						$data_menu_top['user']['avatar'] = $this->facebook_api->get_user_avatar($fb_user_id);
									
					$data_menu_top['user']['name'] = $this->session->userdata('MEM_NAME');
					$data_menu_top['user']['id'] = $fb_user_id;
					$data_menu_top['logged_in'] = true;
				}
			
				$this->template->write('menu_top', $this->load->view('template/component/main/frm_menu_top', $data_menu_top, true));
			}
			if ($logo)
				$this->template->write('header', $this->load->view('template/component/main/frm_header', null, true));
			if ($footer)						
				$this->template->write('footer', $this->load->view('template/component/main/frm_footer', null, true));
		}		
		protected function set_navigation($data){
			$data_content['nav_list'] = $data;
			$this->template->write('nav', $this->load->view('template/component/main/frm_nav', $data_content, true));
		}
				
		protected function check_member_session(){
			$logged_in = $this->session->userdata('IS_LOGGED_IN');
			$member_id = $this->session->userdata('MEM_ID');
			if ($logged_in && !empty($logged_in) && !empty($member_id))
				return true;
			else
				return false;			
		}
		
		protected function is_member_exist(){
			$member_id = $this->session->userdata('MEM_ID');
			if (!empty($member_id)) {
				$this->load->model('local/data/mod_member');
				return $this->mod_member->check_member_exist($member_id);
			} else {
				return false;
			}			
		}
		protected function jscript_redirect($url, $message=null){
			$html = '<script type="text/javascript">';
			if (!is_null($message))
				$html .= 'alert("' . $message . '");';
			$html .= 'window.location.href="' . $url . '";</script>';
			print $html;
		}		
		function ajax_get_category_list(){
			$this->load->model('local/data/cafe/mod_cafe_product_category');
			$result = $this->mod_cafe_product_category->get_category_list();
			
			$return = array();
			if (count($result) > 0){
				foreach ($result as $row){
					array_push($return, array('id' => $row['CAF_PDT_CAT_ID'],
												'name' => $row['CAF_PDT_CAT_NAME']));
				}
			}
				
			print_r(json_encode($return));
		}
		function ajax_get_province_list(){
			$this->load->model('local/data/area/mod_province');
			$result = $this->mod_province->get_province_list(false);

			$return = array();
			if (count($result) > 0){
				foreach ($result as $row){
					array_push($return, array('id' => $row['PROVINCE_ID'],
												'name' => $row['PROVINCE_NAME']));
				}
			}
			
			print_r(json_encode($return));
		}
		protected function cafe_id(){
			$this->load->model('local/data/cafe/mod_cafe');
			return $this->mod_cafe->get_cafe_id_from_name(get_class($this));
		}
		
		// Authen member
		protected function login(){
			$this->load->library('string_function');
			
			$username = $this->input->post('username');
			//$password = $this->string_function->provide_password($this->input->post('password'));
			$password = $this->input->post('password');
									
			$this->load->model('local/data/mod_member');
			$member_row = $this->mod_member->authen($username, $password);
			if (!is_null($member_row))
			{
				$mem_id = $member_row['MEM_ID'];
				$mem_username = $member_row['MEM_TITLE'];
							
				$this->session->set_userdata(array('IS_LOGGED_IN'	=> true,
													'MEM_ID' 		=> $mem_id,
													'USERNAME' 		=> $mem_username,
													'USTATE'		=> 'm'));
				return true;
			}
			else
				return false;			
		}
		protected function logout(){
			$this->session->sess_destroy();
		} 
	}
?>